/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.dataaccess;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
import data.Letter;

/**
 *
 * @author Administrator
 */
public class LetterDAO {
    
    private static Logger logger = Logger.getLogger(LetterDAO.class);
    
    private static ResourceBundle bundle = ResourceBundle.getBundle("server.dataaccess.database");
    
    private LetterDAO(){
    }
    
    public static ArrayList<Letter> getAlleLetters(){
        ArrayList<Letter> letters = new ArrayList<Letter>();
        String opdracht = "{call selectAlleLetters()}";
        
        try {
            CallableStatement cstmt = Database.getInstance().getConnection().prepareCall(opdracht);
            ResultSet rs = cstmt.executeQuery();
            while(rs.next()){
                Letter l = new Letter();
                l.setLetter(rs.getString(bundle.getString("letter_letter")).charAt(0));
                l.setAantalKeerInSpel(rs.getInt(bundle.getString("letter_aantal")));
                l.setPuntenWaarde(rs.getInt(bundle.getString("letter_punten")));
                
                letters.add(l);
            }
            
            cstmt.close();
            logger.info("Alle letters succesvol opgehaald uit de database.");
        } catch (SQLException ex) {
            logger.error("Fout bij het ophalen van de lijst met alle letters!");
            logger.error(ex.getMessage());
        } finally{
            try {
                Database.getInstance().getConnection().close();
            } catch (SQLException ex) {
                logger.error("Fout bij het sluiten van de databaseconnectie!");
                logger.error(ex.getMessage());
            }
        }
        
        return letters;
    }
}
